function clicked_qrcodepay(id, s, t) {
	if($('#sum')) {
		var sum = $('#sum').val();
		if(sum == '') {
			plus.nativeUI.alert('请输入金额');
			return;
		}
		if(!chk(sum)) {
			plus.nativeUI.alert('金额是数字且最多二位小数');
			return;
		}
	}
	clicked(id, s, t);
}

var statud = "";

function dmpay(type, result,price,remark) {
	plus.nativeUI.showWaiting("正在交易中，请勿退出...")
	console.log(remark);
	var sum = $('#sum').val();
	var model = "api/merchant/login";
	var model1 = "api/merchant/MOrder";
	var model2 = "api/merchant/MoneyPay";
	var model3 = "api/merchant/TradeQuery";
	var model4 = "api/merchant/TradePay";
	//去收款
	var times = Date.parse(new Date());
	times = times / 1000;
	var signx=false;
	//需传送的参数
	var datas = {
		code: result,
		token: localStorage.getItem('token'),
		total_amount: price,
		out_trade_no: '45678908' + times,
		remark:remark
	};
	
	if(getSysInfo().search('V1') != -1){
		datas.mac_type='V1';
	}else if(getSysInfo().search('P1') != -1){
		datas.mac_type='P1';
	}else if(getSysInfo().search('JICAI') != -1){
		datas.mac_type='JICAI';
	}else if(getSysInfo().search('iPhone') != -1){
		datas.mac_type='iPhone';
	}else{
		datas.mac_type=getSysInfo();
	}
	
	//普通支付
	mui.post(weburl + model4, datas, function(data) { //请求支付
		console.log(JSON.stringify(datas));
		var tradeDeta = JSON.stringify(data);
		console.log(tradeDeta);
		localStorage.setItem("tradeDetail", tradeDeta); //保存请求到的数据
		setTimeout(function() {
			if(data.status == 1) { //交易成功
				var videoSign = 1;
				//调取交易数据
				tradeDe = JSON.parse(localStorage.getItem("tradeDetail"));
				//给该信息添加自定义属性
				$(".pay_remarks").val("");
				$(".price-cash").val("0.00");
				if(videoSign == 1) {
					localStorage.setItem("xiaopiao", "0");
					//转义请求到的需要打印小票的格式
					var str = tradeDe.print_body;
					var reg = /\\n/g;
					str = str.replace(reg, '\n');
					//打印小票
					//播放成功到账的语音
					video("成功到账" + char_zh(price) + "元"); 
					mui.closePopups();
					printTicket(data.print_array);
					plus.nativeUI.closeWaiting(); //关闭等待框
//					ticket(data.store_name,str,data.out_trade_no);
					mui.confirm("收款成功，是否打印小票？", " ", ["打印", "关闭"], function(e) {
						if(e.index == 0) {
							if(signx == false) {
								printTicket(data.print_array);
//								ticket(data.store_name,str,data.out_trade_no);
								signx = true;
								setTimeout(function() {
									signx = false;
									console.log(signx);
								}, 3000);
							}
							return false;
						}
					}, "div");

					videoSign = 0;
				}
			} else { //如果该付款码是数字，但不是本次支付的付款码
				plus.nativeUI.closeWaiting(); //关闭等待框
				video(data.msg); //播放收款失败的语音
				mui.closePopups();
				mui.alert(data.msg," ","关闭",function(){
					
				},"div");
			}
		}, 1000)
	}, 'json');

}

//数字转化为中文
function char_zh(priceNum) {
	return Arabia_to_Chinese(priceNum);

	function Arabia_to_Chinese(Num) {
		for(i = Num.length - 1; i >= 0; i--) {
			Num = Num.replace(",", "") //替换tomoney()中的“,”
			Num = Num.replace(" ", "") //替换tomoney()中的空格
		}
		Num = Num.replace("￥", "") //替换掉可能出现的￥字符
		if(isNaN(Num)) { //验证输入的字符是否为数字
			alert("请检查小写金额是否正确");
			return;
		}
		//字符处理完毕后开始转换，采用前后两部分分别转换
		part = String(Num).split(".");
		newchar = "";
		//小数点前进行转化
		for(i = part[0].length - 1; i >= 0; i--) {
			if(part[0].length > 10) {
				alert("位数过大，无法计算");
				return "";
			} //若数量超过拾亿单位，提示
			tmpnewchar = ""
			perchar = part[0].charAt(i);
			switch(perchar) {
				case "0":
					tmpnewchar = "零" + tmpnewchar;
					break;
				case "1":
					tmpnewchar = "壹" + tmpnewchar;
					break;
				case "2":
					tmpnewchar = "贰" + tmpnewchar;
					break;
				case "3":
					tmpnewchar = "叁" + tmpnewchar;
					break;
				case "4":
					tmpnewchar = "肆" + tmpnewchar;
					break;
				case "5":
					tmpnewchar = "伍" + tmpnewchar;
					break;
				case "6":
					tmpnewchar = "陆" + tmpnewchar;
					break;
				case "7":
					tmpnewchar = "柒" + tmpnewchar;
					break;
				case "8":
					tmpnewchar = "捌" + tmpnewchar;
					break;
				case "9":
					tmpnewchar = "玖" + tmpnewchar;
					break;
			}
			switch(part[0].length - i - 1) {
				case 0:
					tmpnewchar = tmpnewchar + "点";
					break;
				case 1:
					if(perchar != 0) tmpnewchar = tmpnewchar + "拾";
					break;
				case 2:
					if(perchar != 0) tmpnewchar = tmpnewchar + "佰";
					break;
				case 3:
					if(perchar != 0) tmpnewchar = tmpnewchar + "仟";
					break;
				case 4:
					tmpnewchar = tmpnewchar + "万";
					break;
				case 5:
					if(perchar != 0) tmpnewchar = tmpnewchar + "拾";
					break;
				case 6:
					if(perchar != 0) tmpnewchar = tmpnewchar + "佰";
					break;
				case 7:
					if(perchar != 0) tmpnewchar = tmpnewchar + "仟";
					break;
				case 8:
					tmpnewchar = tmpnewchar + "亿";
					break;
				case 9:
					tmpnewchar = tmpnewchar + "拾";
					break;
			}
			newchar = tmpnewchar + newchar;
		}
		//小数点之后进行转化
		if(Num.indexOf(".") != -1) {
			if(part[1].length > 2) {
				alert("小数点之后只能保留两位,系统将自动截断");
				part[1] = part[1].substr(0, 2)
			}

			for(i = 0; i < part[1].length; i++) {
				tmpnewchar = ""
				perchar = part[1].charAt(i)
				switch(perchar) {
					case "0":
						tmpnewchar = "零" + tmpnewchar;
						break;
					case "1":
						tmpnewchar = "壹" + tmpnewchar;
						break;
					case "2":
						tmpnewchar = "贰" + tmpnewchar;
						break;
					case "3":
						tmpnewchar = "叁" + tmpnewchar;
						break;
					case "4":
						tmpnewchar = "肆" + tmpnewchar;
						break;
					case "5":
						tmpnewchar = "伍" + tmpnewchar;
						break;
					case "6":
						tmpnewchar = "陆" + tmpnewchar;
						break;
					case "7":
						tmpnewchar = "柒" + tmpnewchar;
						break;
					case "8":
						tmpnewchar = "捌" + tmpnewchar;
						break;
					case "9":
						tmpnewchar = "玖" + tmpnewchar;
						break;
				}
				if(i == 0) tmpnewchar = tmpnewchar + "";
				if(i == 1) tmpnewchar = tmpnewchar + "";
				newchar = newchar + tmpnewchar;
			}
		}

		//替换所有无用汉字
		while(newchar.search("零零") != -1)
			newchar = newchar.replace("零零", "零");
		newchar = newchar.replace("零亿", "亿");
		newchar = newchar.replace("亿万", "亿");
		newchar = newchar.replace("零万", "万");
		newchar = newchar.replace("零点", "零点");
		newchar = newchar.replace("零", "零");
		newchar = newchar.replace("零", "零");
		if(newchar.charAt(newchar.length - 1) == "元" || newchar.charAt(newchar.length - 1) == "角")
			newchar = newchar;
		return newchar;
	}

	function printp(obj) {
		if(obj.style.display == "none") {
			obj.style.display = "";
		} else {
			obj.style.display = "none";
		}
	}

}

//语音播报功能
function video(info) {
	var zhText = info;
	zhText = encodeURI(zhText);
	var str = "<audio autoplay=\"autoplay\"><source src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=5&text=" + zhText + "\" type=\"audio/mpeg\"><embed height=\"0\" width=\"0\" src=\"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=5&text=" + zhText + "\"></audio>"
	$("body").append(str);
}

//验证是不是付款码
function verify_codepay(result) {
	var numarr = [10, 11, 12, 13, 14, 15, 18, 28, 31];
	if(!checkNumber(result)) {
		return false;
	}
	console.log("通过");
	var str = result.substring(0, 2); //取得前2位
	console.log(str);
	if(contains(numarr, str)) {
		return true;
	} else {
		return false;
	}
}

//验证字符串是否是数字
function checkNumber(theObj) {
	var reg = /^[0-9]*$/;
	if(reg.exec(theObj)) {
		return true;
	}
	return false;
}
/**
 * 某元款是否存在于数组中
 * @param {Object} arr 数组
 * @param {Object} obj 元款
 */
function contains(arr, obj) {
	var i = arr.length;
	while(i--) {
		if(arr[i] == obj) {
			return true;
		}
	}
	return false;
}

/**
 * 验证是不是整数或者小数
 * @param {Object} res
 */
function chk(res) {
	var patrn = /^[0-9]+([.]{1}[0-9]{1,2})?$/;
	if(patrn.exec(res)) {
		return true;
	}
	return false;
}
$(document).ready(function() {
	function getUserInfo() {
		mui.init();
		//订单数据
		var sum = $('#sum').val();
		var model = "api/merchant/login";
		var model3 = "api/merchant/TradeQuery";
		//登陆获取token
		if(!localStorage.getItem('token')) { //如果token不存在
			mui.post(weburl + model, {
				phone: "15058132455",
				password: "718000"
			}, function(data) {
				var datastr = JSON.stringify(data);
				localStorage.setItem('token', data.token); //把获取的token 存起来
			}, 'json');
		}
	}
	getUserInfo();
})